package com.yeqifu.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yeqifu.bus.entity.Goods;
import com.yeqifu.bus.entity.GoodsDate;
import com.yeqifu.bus.entity.Inport;
import com.yeqifu.bus.entity.Outport;
import com.yeqifu.bus.mapper.GoodsDateMapper;
import com.yeqifu.bus.mapper.GoodsMapper;
import com.yeqifu.bus.mapper.InportMapper;
import com.yeqifu.bus.service.IInportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeqifu.bus.vo.InportVo;
import com.yeqifu.sys.entity.Shelf;
import com.yeqifu.sys.mapper.ShelfMapper;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * InnoDB free: 9216 kB; (`providerid`) REFER `warehouse/bus_provider`(`id`); (`goo 服务实现类
 * </p>
 *
 * @author 艾琦
 * @since 2020-12-18
 */
@Service
@AllArgsConstructor
@Transactional
public class InportServiceImpl extends ServiceImpl<InportMapper, Inport> implements IInportService {

    GoodsMapper goodsMapper;
    InportMapper inportMapper;
    GoodsDateMapper goodsDateMapper;
    ShelfMapper shelfMapper;

    /**
     * 业务管理员修改订单信息
     * @param inport
     * @return
     */
    @Override
    public boolean save(InportVo inport) {
        return super.save(inport);
    }

    @Override
    public boolean recallById(Inport entity, int type) {
        //根据进货ID查询进货信息
        Inport inport = baseMapper.selectById(entity.getId());
        int status = inport.getStatus();
        // 判断是否是业务管理员 且 未处理 || 是否是仓库管理员 且 未处理/已处理
        if ((type == 0 && status == 0) || (type == 1 && (status == 1 || status == 0)))
            return super.updateById(entity);
            // 判断
        else
            return false;
    }
    /**
     * 业务管理员修改订单信息
     * @param inportvo
     * @return
     */
    @Override
    public boolean update(InportVo inportvo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",inportvo.getId());
        Inport inport = inportMapper.selectById(inportvo.getId());
        // 判断状态是否是未处理，不是则返回 false
        if (inport.getStatus() != 0) {
            return false;
        }
        return super.save(inport);
    }


    /**
     * 仓库管理员分配货架
     * @param inportvo
     * @return
     */
    @Override
    public boolean saveShelf(InportVo inportvo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",inportvo.getId());
        Inport inport = inportMapper.selectById(inportvo.getId());
        // 判断状态是否是处理中和未处理，不是则返回 false
        if (inport.getStatus() != 0 && inport.getStatus() != 1) {
            return false;
        }
        inport.setStatus(1);
        return super.updateById(inport);
    }

    /**
     * 保存商品进货
     * 是实际的商品入库，由仓管员进行操作，只能入库“处理中”的商品
     * @param inportvo
     * @return
     */
    @Override
    public boolean realSave(InportVo inportvo) {
        Inport inport = inportMapper.selectById(inportvo.getId());
        // 判断状态是否是处理中
        if (inport.getStatus() != 1) {
            return false;
        }
        // 修改状态
        inport.setStatus(2);
        inportMapper.updateById(inport);
        // 根据商品ID查询商品
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        // 查询到商品后，查询到是在已有的货架有商品
        if (goods.getLocationid() == inport.getShelfId()) {
            goods.setNumber(goods.getNumber()+inport.getNumber());
            goodsMapper.updateById(goods);
        } else {        // 如果查询到的id不属于这个仓库，则在商品表中添加项
            Goods newGood = new Goods();
            newGood.setGoodsname(goods.getGoodsname());
            newGood.setProviderid(inport.getProviderid());
            newGood.setType(inport.getType());
            newGood.setSize(inport.getSize());
            newGood.setAreaid(inport.getWareHousePName());
            newGood.setDepotid(inport.getWareHouseName());
            newGood.setLocationid(inport.getShelfId());
            newGood.setPrice(inport.getInportprice());
            newGood.setNumber(inport.getNumber());
            newGood.setAvailable(1);
            goodsMapper.insert(newGood);
        }
        // 无论如何，都会将商品的保质期信息添加到 bus_goods_data 中 ORDY BY
        GoodsDate goodsDate = new GoodsDate();
        goodsDate.setGoodsid(inport.getGoodsid());
        goodsDate.setAreaid(inport.getWareHousePName());
        goodsDate.setDepotid(inport.getWareHouseName());
        goodsDate.setName(goods.getGoodsname());
        goodsDate.setProdate(inport.getProdate());
        goodsDate.setQualdate(inport.getQualdate());
        goodsDate.setNum(inport.getNumber());
        // 保存货架位置
        goodsDate.setLocationid(inport.getShelfId());
        // 根据货架ID 找到货架位置
        Shelf shelf = shelfMapper.selectById(inport.getShelfId());
        // 将对应货架位置的数量++
        shelf.setCapacity(shelf.getCapacity() + inport.getNumber());
        shelfMapper.updateById(shelf);
        // 插入商品生产日期信息
        goodsDateMapper.insert(goodsDate);

        return true;
    }

    /**
     * 业务管理员更新商品进货
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(Inport entity) {
        //根据进货ID查询进货信息
        Inport inport = baseMapper.selectById(entity.getId());
        // 判断状态，是否是“未处理的状态”
        if (inport.getStatus() != 0)
            return false;
        //更新进货单
        return super.updateById(entity);
    }

    /**
     * 删除商品进货信息
     * @param id
     * @return
     */
    @Override
    public boolean removeById(Serializable id) {
        //根据进货ID查询进货信息
        Inport inport = baseMapper.selectById(id);
        //根据商品ID查询商品信息
        Goods goods = goodsMapper.selectById(inport.getGoodsid());
        //库存算法  当前库存-进货单数量
        goods.setNumber(goods.getNumber()-inport.getNumber());
        goodsMapper.updateById(goods);
        //更新商品的数量
        return super.removeById(id);
    }

    /**
     * 查询商品年出库情况(近似于销售情况)
     * @return
     */
    @Override
    public List<Inport> loadYearSales(Integer id) {
        List<Inport> inport = baseMapper.loadYearSales(id);
        return inport;
    }
}
